package com.wise.service.cms.content.pagedecorate;

import com.wise.entities.content.Decorate;
import com.wise.entities.content.DecorateType;
import com.wise.framework.basic.DateTimeUtil;
import com.wise.framework.daomodel.DaoOne;
import com.wise.framework.daomodel.DaoSave;
import com.wise.framework.exception.LogicException;
import com.wise.framework.session.CurrentUserId;
import com.wise.summer.v1.cms.content.pagedecorate.CopyPageDecorate;
import org.springframework.stereotype.Service;
import org.summerframework.model.SummerService;
import org.summerframework.model.SummerServiceBean;

import java.sql.Timestamp;

import static com.wise.model.content.ContentErrMsgs.Err_000500000008;
import static com.wise.model.content.ContentErrMsgs.Err_000500000027;


@Service
@SummerService
public class CopyPageDecorateService implements SummerServiceBean<CopyPageDecorate> {

    @Override
    public void sum(CopyPageDecorate model) {
        Decorate dr = DaoOne.s(Decorate.class, model.getId(), Err_000500000008);
        // 别名转换
        model.setAlias(PageDecorateTools.toNullAlias(model.getAlias()));
        // 别名不许重复
        if (PageDecorateTools.checkAlias(model.getAlias())) {
            throw new LogicException(this.getClass(), Err_000500000027);
        }

        Timestamp now = DateTimeUtil.timestampNow();

        Decorate entity = new Decorate();
        entity.setAlias(model.getAlias());
        entity.setLang(dr.getLang());
        entity.setImg(model.getImg());
        entity.setTitle(model.getTitle());
        entity.setContent(dr.getContent());
        entity.setPublicContent(dr.getPublicContent());
        entity.setModule(dr.getModule());
        entity.setType(DecorateType.PC.value());

        entity.setCreatedBy(CurrentUserId.s(true));
        entity.setCreationDate(now);

        DaoSave.s(entity);
        model.setSummerResult(entity);
    }
}
